package com.alander.nexusgo.post.service.impl;

import com.alander.nexusgo.common.model.dto.PostsListDTO;
import com.alander.nexusgo.common.model.vo.PostsVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.alander.nexusgo.post.model.entity.Posts;
import com.alander.nexusgo.post.service.PostsService;
import com.alander.nexusgo.post.mapper.PostsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author alander
*/
@Service
public class PostsServiceImpl extends ServiceImpl<PostsMapper, Posts>
    implements PostsService{
    
    @Override
    public PostsVO createPost(Posts posts, Long userId) {
        posts.setUserId(userId);
        this.save(posts);
        return convertToVO(posts);
    }
    
    @Override
    public PostsVO updatePost(Long id, Posts posts, Long userId) {
        Posts existing = this.getById(id);
        if (existing == null) {
            throw new RuntimeException("帖子不存在");
        }
        
        if (!existing.getUserId().equals(userId)) {
            throw new RuntimeException("无权限修改他人帖子");
        }
        
        posts.setId(id);
        this.updateById(posts);
        return convertToVO(this.getById(id));
    }
    
    @Override
    public boolean deletePost(Long id, Long userId) {
        Posts existing = this.getById(id);
        if (existing == null) {
            throw new RuntimeException("帖子不存在");
        }
        
        if (!existing.getUserId().equals(userId)) {
            throw new RuntimeException("无权限删除他人帖子");
        }
        
        // 软删除
        LambdaUpdateWrapper<Posts> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Posts::getId, id)
                .set(Posts::getDelFlag, "1");
        return this.update(updateWrapper);
    }
    
    @Override
    public PostsVO getPostById(Long id) {
        Posts posts = this.getById(id);
        return posts != null ? convertToVO(posts) : null;
    }
    
    @Override
    public List<PostsVO> listPosts(PostsListDTO postsListDTO) {
        LambdaQueryWrapper<Posts> queryWrapper = new LambdaQueryWrapper<>();
        
        if (postsListDTO.getUserId() != null) {
            queryWrapper.eq(Posts::getUserId, postsListDTO.getUserId());
        }
        
        if (postsListDTO.getPostType() != null) {
            queryWrapper.eq(Posts::getPostType, postsListDTO.getPostType());
        }
        
        if (postsListDTO.getKeyword() != null && !postsListDTO.getKeyword().isEmpty()) {
            queryWrapper.and(wrapper -> wrapper
                    .like(Posts::getTitle, postsListDTO.getKeyword())
                    .or()
                    .like(Posts::getContent, postsListDTO.getKeyword()));
        }
        
        if (postsListDTO.getIsTop() != null) {
            queryWrapper.eq(Posts::getIsTop, postsListDTO.getIsTop());
        }
        
        if (postsListDTO.getIsEssence() != null) {
            queryWrapper.eq(Posts::getIsEssence, postsListDTO.getIsEssence());
        }
        
        if (postsListDTO.getStatus() != null) {
            queryWrapper.eq(Posts::getStatus, postsListDTO.getStatus());
        }
        
        // 只查询未删除的帖子
        queryWrapper.eq(Posts::getDelFlag, "0");
        
        queryWrapper.orderByDesc(Posts::getCreateTime);
        List<Posts> postsList = this.list(queryWrapper);
        
        return postsList.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }
    
    /**
     * 将Posts实体转换为PostsVO
     * @param posts Posts实体
     * @return PostsVO对象
     */
    private PostsVO convertToVO(Posts posts) {
        PostsVO postsVO = new PostsVO();
        BeanUtils.copyProperties(posts, postsVO);
        if (posts.getCreateTime() != null) {
            postsVO.setCreateTime(posts.getCreateTime().toString());
        }
        if (posts.getUpdateTime() != null) {
            postsVO.setUpdateTime(posts.getUpdateTime().toString());
        }
        return postsVO;
    }
}